/*
 * Copyright 2009-2010 the original author or authors.
 *
 * Licensed under the Apache license, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.internna.iwebjtracker.model;

import java.util.Locale;
import javax.persistence.Table;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQuery;
import org.hibernate.validator.NotNull;
import org.internna.iwebmvc.utils.Assert;
import org.internna.iwebjtracker.issue.model.Issue;
import org.internna.iwebmvc.model.AbstractDomainEntity;
import org.internna.iwebjtracker.issue.model.Issue.Link;
import org.directwebremoting.annotations.DataTransferObject;

/**
 * Defines a link between two existing issues.
 *
 * @author Jose Noheda
 * @since 1.0
 */
@Entity
@Table(name = "INTERLINKS")
@DataTransferObject(javascript = "Interlink")
@NamedQuery(name = "Interlink.findAll", query = "SELECT i FROM Interlink i")
public class Interlink extends AbstractDomainEntity {

    private static final long serialVersionUID = 1722478379283839208L;

    /**
     * Default constructor for Hibernate instantiation.
     */
    public Interlink() {
        super();
    }

    /**
     * Convenience constructor.
     *
     * @param origin any non null instance
     * @param target any non null instance
     * @param type any non null instance
     */
    public Interlink(DefaultIssue origin, DefaultIssue target, Issue.Link type) {
        super();
        Assert.notNull(type);
        Assert.notNull(origin);
        Assert.notNull(target);
        this.type = type;
        this.issue = origin;
        this.target = target;
    }

    @NotNull
    @Column(name = "TYPE", updatable = false)
    private Issue.Link type;

    @NotNull
    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "ISSUE", updatable = false)
    private DefaultIssue issue;

    @NotNull
    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "TARGET", updatable = false)
    private DefaultIssue target;

    /**
     * The origin of the relation.
     *
     * @return a not null origin
     */
    public DefaultIssue getIssue() {
        return issue;
    }

    /**
     * Accessor method.
     *
     * @param issue any
     */
    public void setIssue(DefaultIssue issue) {
        Assert.notNull(issue);
        this.issue = issue;
    }

    /**
     * The issue related.
     *
     * @return a non null instance
     */
    public DefaultIssue getTarget() {
        return target;
    }

    /**
     * Accessor method.
     *
     * @param target any
     */
    public void setTarget(DefaultIssue target) {
        Assert.notNull(target);
        this.target = target;
    }

    /**
     * The kind of relation between the issues.
     *
     * @return a not null instance
     */
    public Link getType() {
        return type;
    }

    /**
     * Accessor method.
     *
     * @param type any
     */
    public void setType(Link type) {
        Assert.notNull(type);
        this.type = type;
    }

    /**
     * String representation for use in UI widgets.
     *
     * @param locale any
     * @return delegates to
     */
    @Override public String getCaption(Locale locale) {
        return issue.getCaption(locale) + " " + type + " " + target.getCaption(locale);
    }

    /**
     * Interlinks are equal if their properties are equal.
     *
     * @param obj any
     * @return
     */
    @Override public boolean equals(Object obj) {
        if ((type != null) && (issue != null) && (target != null) && (obj instanceof Interlink)) {
            Interlink linked = (Interlink) obj;
            return issue.equals(linked.getIssue()) && target.equals(linked.target) && type.equals(linked.type);
        }
        return super.equals(obj);
    }

    /**
     * Creates a hashcode using the sum of the hashcodes of its properties or falls back to super.
     *
     * @return issue + type + target hashcodes if configured
     */
    @Override public int hashCode() {
        return type != null && issue != null && target != null ? issue.hashCode() + target.hashCode() + type.hashCode() : super.hashCode();
    }

}
